package com.hd.pzhwlxt.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.pzhwlxt.mapper.OfficeMaterialPurchaseMapper;
import com.hd.pzhwlxt.pojo.OfficeMaterial;
import com.hd.pzhwlxt.pojo.OfficeMaterialPurchase;
import com.hd.pzhwlxt.service.OfficeMaterialPurchaseService;
import com.hd.pzhwlxt.service.OfficeMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @create: 2022-09-23 11:22
 * @author: 怀念天空
 * @Description:
 */

@Service
public class OfficeMaterialPurchaseServiceImpl extends ServiceImpl<OfficeMaterialPurchaseMapper, OfficeMaterialPurchase> implements OfficeMaterialPurchaseService {

    @Autowired
    private OfficeMaterialPurchaseMapper officeMaterialPurchaseMapper;

    @Autowired
    private OfficeMaterialService officeMaterialService;

    @Override
    public boolean addPurchaseInfo(OfficeMaterialPurchase officeMaterialPurchase) {
        //根据id获取详细信息，得到单价
        Double price = officeMaterialService.getById(officeMaterialPurchase.getMaterialId()).getPrice();
        officeMaterialPurchase.setMoney(BigDecimal.valueOf(officeMaterialPurchase.getPurchaseNum() * price));
        return officeMaterialPurchaseMapper.insert(officeMaterialPurchase) > 0 ;
    }

    @Override
    public boolean updatePurchaseInfo(OfficeMaterialPurchase officeMaterialPurchase) {
        OfficeMaterial material = officeMaterialService.getById(officeMaterialPurchase.getMaterialId());
        //更新库存
        material.setCount(material.getCount() + officeMaterialPurchase.getPurchaseNum());
        officeMaterialService.updateById(material);
        //更新采购状态
        officeMaterialPurchase.setStatus(1);
        return officeMaterialPurchaseMapper.updateById(officeMaterialPurchase) > 0;
    }
}

